<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>errorhandler_async_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_debug_errorhandler_async_test.js.html">errorhandler_async_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.debug.ErrorHandlerAsyncTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.debug.ErrorHandlerAsyncTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.debug.ErrorHandler&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line22"></a>
<a name="line23"></a>var testCase = new goog.testing.AsyncTestCase(document.title);
<a name="line24"></a>
<a name="line25"></a>testCase.setUpPage = function() {
<a name="line26"></a>  this.waitForAsync(&#39;setUpPage&#39;);
<a name="line27"></a>  this.stepTimeout = 5 * 1000;
<a name="line28"></a>
<a name="line29"></a>  this.oldTimeout = window.setTimeout;
<a name="line30"></a>  this.oldInterval = window.setInterval;
<a name="line31"></a>  this.oldRequestAnimationFrame = window.requestAnimationFrame;
<a name="line32"></a>
<a name="line33"></a>  // Whether requestAnimationFrame is available for testing.
<a name="line34"></a>  this.testingReqAnimFrame = !!window.requestAnimationFrame;
<a name="line35"></a>
<a name="line36"></a>  this.handler = new goog.debug.ErrorHandler(
<a name="line37"></a>      goog.bind(this.onException, this));
<a name="line38"></a>  this.handler.protectWindowSetTimeout();
<a name="line39"></a>  this.handler.protectWindowSetInterval();
<a name="line40"></a>  this.handler.protectWindowRequestAnimationFrame();
<a name="line41"></a>  this.exceptions = [];
<a name="line42"></a>  this.errors = 0;
<a name="line43"></a>
<a name="line44"></a>  // Override the error event handler, since we are purposely throwing
<a name="line45"></a>  // exceptions from global functions, and expect them
<a name="line46"></a>  this.oldWindowOnError = window.onerror;
<a name="line47"></a>  window.onerror = goog.bind(this.onError, this);
<a name="line48"></a>
<a name="line49"></a>  window.setTimeout(goog.bind(this.timeOut, this), 10);
<a name="line50"></a>  this.intervalId = window.setInterval(goog.bind(this.interval, this), 20);
<a name="line51"></a>
<a name="line52"></a>  if (this.testingReqAnimFrame) {
<a name="line53"></a>    window.requestAnimationFrame(goog.bind(this.animFrame, this));
<a name="line54"></a>  }
<a name="line55"></a>};
<a name="line56"></a>
<a name="line57"></a>testCase.tearDownPage = function() {
<a name="line58"></a>  window.setTimeout = this.oldTimeout;
<a name="line59"></a>  window.setInterval = this.oldInterval;
<a name="line60"></a>  window.requestAnimationFrame = this.oldRequestAnimationFrame;
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>testCase.onException = function(e) {
<a name="line64"></a>  this.exceptions.push(e);
<a name="line65"></a>  if (this.timeoutHit &amp;&amp; this.intervalHit &amp;&amp;
<a name="line66"></a>      (!this.testingReqAnimFrame || this.animFrameHit)) {
<a name="line67"></a>    this.continueTesting();
<a name="line68"></a>  }
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>testCase.onError = function(msg, url, line) {
<a name="line72"></a>  this.errors++;
<a name="line73"></a>  return true;
<a name="line74"></a>};
<a name="line75"></a>
<a name="line76"></a>testCase.timeOut = function() {
<a name="line77"></a>  this.timeoutHit = true;
<a name="line78"></a>  throw arguments.callee;
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>testCase.interval = function() {
<a name="line82"></a>  this.intervalHit = true;
<a name="line83"></a>  window.clearTimeout(this.intervalId);
<a name="line84"></a>  throw arguments.callee;
<a name="line85"></a>};
<a name="line86"></a>
<a name="line87"></a>testCase.animFrame = function() {
<a name="line88"></a>  this.animFrameHit = true;
<a name="line89"></a>  throw arguments.callee;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>testCase.addNewTest(&#39;testResults&#39;, function() {
<a name="line93"></a>  var timeoutHit, intervalHit, animFrameHit;
<a name="line94"></a>
<a name="line95"></a>  for (var i = 0; i &lt; this.exceptions.length; ++i) {
<a name="line96"></a>    switch (this.exceptions[i]) {
<a name="line97"></a>      case this.timeOut: timeoutHit = true; break;
<a name="line98"></a>      case this.interval: intervalHit = true; break;
<a name="line99"></a>      case this.animFrame: animFrameHit = true; break;
<a name="line100"></a>    }
<a name="line101"></a>  }
<a name="line102"></a>
<a name="line103"></a>  assertTrue(&#39;timeout exception not received&#39;, timeoutHit);
<a name="line104"></a>  assertTrue(&#39;timeout not called&#39;, this.timeoutHit);
<a name="line105"></a>  assertTrue(&#39;interval exception not received&#39;, intervalHit);
<a name="line106"></a>  assertTrue(&#39;interval not called&#39;, this.intervalHit);
<a name="line107"></a>  if (this.testingReqAnimFrame) {
<a name="line108"></a>    assertTrue(&#39;anim frame exception not received&#39;, animFrameHit);
<a name="line109"></a>    assertTrue(&#39;animFrame not called&#39;, this.animFrameHit);
<a name="line110"></a>  }
<a name="line111"></a>
<a name="line112"></a>  if (!goog.userAgent.WEBKIT) {
<a name="line113"></a>    var expectedRethrownCount = this.testingReqAnimFrame ? 3 : 2;
<a name="line114"></a>    assertEquals(
<a name="line115"></a>        expectedRethrownCount + &#39; exceptions should have been rethrown&#39;,
<a name="line116"></a>        expectedRethrownCount, this.errors);
<a name="line117"></a>  }
<a name="line118"></a>});
<a name="line119"></a>
<a name="line120"></a>// Standalone Closure Test Runner.
<a name="line121"></a>G_testRunner.initialize(testCase);
</pre>


</body>
</html>
